package com.qc.wintrax.utils;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.OutputStream;

import static com.qc.wintrax.R.id.imageView;

/**
 * Created by Administrator on 2016/8/12.
 */
public class PatinImageUtil {
    private static float downX = 0;
    private static float downY = 0;
    private static float upX = 0;
    private static float upY = 0;
    private static Canvas canvas;
    private static Paint paint;
    private static Bitmap bmp;
    private static Bitmap drawBitmap;

    private static Canvas createCanvas(Bitmap bitmap) {
        Canvas tmpCanvas = new Canvas(bitmap);
        return tmpCanvas;
    }

    private static Paint creatPaint() {
        Paint tmpPaint = new Paint();
        tmpPaint.setColor(Color.RED);
        tmpPaint.setStrokeWidth(5);
        return tmpPaint;
    }

    private static Matrix createMatrix() {
        Matrix matrix = new Matrix();
        return matrix;
    }

    public static Bitmap loadBitmap(Activity context, Uri uri) {
        Bitmap bitmap = null;
        try {
            DisplayMetrics metrics = new DisplayMetrics();
            context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
            int dw = metrics.widthPixels;
            int dh = metrics.heightPixels;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri), null, options);
            int heightRadio = (int) Math.ceil(options.outHeight / dh);
            int widthRadio = (int) Math.ceil(options.outWidth / dw);
            if (heightRadio > 1 && widthRadio > 1) {
                if (heightRadio > widthRadio) {
                    options.inSampleSize = heightRadio;
                } else {
                    options.inSampleSize = widthRadio;
                }
            }
            options.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri), null, options);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    public static void patinImg(Activity activity, Uri uri, final ImageView imageView) {
        bmp = loadBitmap(activity, uri);
        drawBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
        imageView.setImageBitmap(drawBitmap);
        canvas = createCanvas(drawBitmap);
        paint = creatPaint();
        canvas.drawBitmap(bmp, createMatrix(), paint);
        imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_CANCEL:
                        break;
                    case MotionEvent.ACTION_DOWN:
                        downX = event.getX();
                        downY = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        upX = event.getX();
                        upY = event.getY();
                        canvas.drawLine(downX, downY, upX, upY, paint);
                        imageView.invalidate();
                        downX = upX;
                        downY = upY;
                        break;
                    case MotionEvent.ACTION_UP:
                        upX = event.getX();
                        upY = event.getY();
                        canvas.drawLine(downX, downY, upX, upY, paint);
                        imageView.invalidate();
                        break;
                    default:
                        break;
                }
                return true;

            }
        });
    }


    public static void setImg(Activity activity, ImageView imageView) {
        if (drawBitmap != null) {
            try {
                Uri imageUri = activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
                OutputStream os = activity.getContentResolver().openOutputStream(imageUri);
                /**
                 *  compress方法将图片转换成JPG或者PNG格式
                 *PNG非常适合艺术线条和图形，JPG适合带渐变的全彩图像，如照片
                 *第二个参数为质量设置，只有当格式为jpg时该参数才会生效，PNG将始终保持所有的数据，从而使质量设置无效
                 *质量设置越大，图片越大，效果越好。
                 *
                 */
                drawBitmap.compress(Bitmap.CompressFormat.JPEG, 90, os);
                imageView.setImageBitmap(drawBitmap);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}
